<?php

class Validate_EmailUnique extends Zend_Validate_Abstract
{
	private $type;
	const RECORD_FOUND = 'recordFound';
	
	public function __construct($type = 'profile')
	{
		$this->type = $type;
	}
	
	protected $_messageTemplates = array(
				self::RECORD_FOUND => 'Данный Email уже зарегистрирован в базе'
		);
	
		public function isValid($value)
		{
			$this->_setValue($value);
			
			$peer =  $this->type == 'subscribe' ? SubscribePeer::getInstance() : ProfilePeer::getInstance();
			$select = $peer->select();
			$select->where("email = ?", $value);
			$row = $peer->fetchRow($select);
				
				if($row){
						$this->_error('recordFound');
						return false;
				}
				
				return true;
		}
}
